
app.get("/login",function (req,res) {
    M.doSql(`select * from user where username='${req.params.username}'`).then(d=>{
        if(d.length==0){
            res.send(M.result("账号或密码错误",false))
        }else{
            if(d[0].password==req.params.password){
                res.send(M.result("登陆成功"))
            }else{
                res.send(M.result("账号或密码错误",false))
            }
        }
    })
})



app.get("/listAllResource",function (req,res) {
    let whereCase="1=1 ";
    if(req.params.name){
        whereCase=whereCase+` and name like '%${req.params.name}%'`
    }
    if(req.params.parent_id){
        whereCase=whereCase+` and parent_id = '${req.params.parent_id}'`
    }
    const sql1=` 
    select * from resource  where ${whereCase}  limit  ${(req.params.startPage-1)*req.params.limit},${req.params.limit}`
    const sql2= `
        select count(1) c from resource  where ${whereCase};
    `;
    M.doSql(sql1).then((d) => {
        let rows = d;
        M.doSql(sql2).then((d) => {
            let total = d[0].c;
            res.send({rows ,total});
        });
    })
})

app.post("/resourceDelete",function (req,res) {
    let ids=req.params.ids.toString();
    let sql=`DELETE from resource WHERE id in (${ids})`
    M.doSql(sql).then(d=>{
        res.send("ok");
    })
});

app.post("/resourceAdd",function (req,res) {
    let resource=req.params;
    delete resource.id;
    let sql =Db.getInsertObjSql("resource",resource) + ";";
    M.doSql(sql).then(d=>{
        res.send("ok");
    })
});

app.post("/resourceUpdate",function (req,res) {
    let resource=req.params;
    let sql =Db.getUpdateObjSql("resource",resource) + ";";
    M.doSql(sql).then(d=>{
        res.send("ok");
    })
});










app.get("/listAllUser",function (req,res) {
    let whereCase="1=1 ";
    if(req.params.username){
        whereCase=whereCase+` and username like '%${req.params.username}%'`
    }
    const sql1=` 
    select * from user  where ${whereCase}  limit  ${(req.params.startPage-1)*req.params.limit},${req.params.limit}`
    const sql2= `
        select count(1) c from user  where ${whereCase};
    `;
    M.doSql(sql1).then((d) => {
        let rows = d;
        M.doSql(sql2).then((d) => {
            let total = d[0].c;
            res.send({rows ,total});
        });
    })
})



app.post("/userAdd",function (req,res) {
    let user=req.params;
    delete user.id;
    let sql =Db.getInsertObjSql("user",user) + ";";
    M.doSql(sql).then(d=>{
        res.send("ok");
    })
});

app.post("/userUpdate",function (req,res) {
    let user=req.params;
    let sql =Db.getUpdateObjSql("user",user) + ";";
    M.doSql(sql).then(d=>{
        res.send("ok");
    })
});



app.post("/userDelete",function (req,res) {
    let ids=req.params.ids.toString();
    let sql=`DELETE from user WHERE id in (${ids})`
    M.doSql(sql).then(d=>{
        res.send("ok");
    })
});




app.get("/listAll",function (req,res) {
    M.doSql("select id from resource").then(d=>{
        d=d.map(d=>d.id)
        res.send(d)
    })
})

app.get("/add",function (req,res) {
    M.doSql("select id from resource").then(d=>{
        d=d.map(d=>d.id)
        res.send(d)
    })
})